from xml.etree.ElementTree import ElementTree
from logger import CLogger
import os
import time

class CParam:
    def __init__(self):
        self.m_name = ''
        self.m_type = ''
        return 
        
    def __del__(self):
        return
    
class CParamList:
    def __init__(self):
        self.m_params = []
        self.m_minNum = 0
        self.m_maxNum = 0
        return 
        
    def __del__(self):
        return

class CSubHelp:
    def __init__(self):
        self.m_name = ''
        self.m_helpInfo = ''
        return 
        
    def __del__(self):
        return

class CHelp:
    def __init__(self):
        self.m_helpInfo = ''
        self.m_subHelps = []
        return 
        
    def __del__(self):
        return
        
class CExeItem:
    def __init__(self):
        self.m_paramList = CParamList()
        self.m_name = ''
        self.m_cmd = ''
        self.m_help = CHelp()
        return 
        
    def __del__(self):
        return
    
class CExeList:
    def __init__(self):
        self.m_exeDict = {}
        return 
        
    def __del__(self):
        return
    
    def loadFromXmlFile(self,strXmlFile):
        CLogger.writeInfLog("load from exe config:%s"%(strXmlFile))
        tree = ElementTree()
        tree.parse(strXmlFile)
        root=tree.getroot() 
        for exeItemNode in root:
            CLogger.writeInfLog("=========================")
            CLogger.writeInfLog(" load exe name:%s cmd:%s"%(exeItemNode.attrib['name'],exeItemNode.attrib['cmd']))
            exeItem = CExeItem()
            exeItem.m_name = exeItemNode.attrib['name']
            exeItem.m_cmd = exeItemNode.attrib['cmd']
            
            CLogger.writeInfLog("  load paramlist info summery")
            paramListNode = exeItemNode.find("paramList")
            exeItem.m_paramList.m_minNum = paramListNode.attrib['numMin']
            exeItem.m_paramList.m_maxNum = paramListNode.attrib['numMax']
            for paramItemNode in paramListNode:
                CLogger.writeInfLog("   load parm name:%s type:%s"%(paramItemNode.attrib['name'],paramItemNode.attrib['type']))
                param = CParam()
                param.m_name = paramItemNode.attrib['name']
                param.m_type = paramItemNode.attrib['type']
                exeItem.m_paramList.m_params.append(param)

            helpNode = exeItemNode.find("help")
            CLogger.writeInfLog("  load help info summery:%s"%(helpNode.attrib['summery']))
            exeItem.m_help.m_helpInfo = helpNode.attrib['summery']
            for subHelpNode in helpNode:
                CLogger.writeInfLog("   load sub help name:%s"%(subHelpNode.attrib['name']))
                subHelp = CSubHelp()
                subHelp.m_name = subHelpNode.attrib['name']
                subHelp.m_helpInfo = subHelpNode.attrib['summery']
                exeItem.m_help.m_subHelps.append(subHelp)
            
            self.m_exeDict[exeItem.m_cmd] = exeItem
        
class CExeInvoker:
    def __init__(self,exeList):
        self.m_list = exeList
        return 
        
    def __del__(self):
        return

    def helpAllExe(self):
        print "%-20s  %-20s  %-20s"%("name","cmd","help")
        print "====================  ====================  ===================="
        for exeItem in self.m_list.m_exeDict.values():
            print "%-20s  %-20s  %-20s"%(exeItem.m_name,exeItem.m_cmd,exeItem.m_help.m_helpInfo)
            
    def runOneExe(self,exeCmdName,exeCmdParams):
        strCmdParam = ''
        for oneParam in exeCmdParams:
            strCmdParam = strCmdParam + ' ' + oneParam
        os.system('%s %s'%(exeCmdName,strCmdParam))
    
    def helpOneExe(self,exeName): 
        print '%s : %s'%(exeName,self.m_list.m_exeDict[exeName].m_help.m_helpInfo)
        for subHelp in self.m_list.m_exeDict[exeName].m_help.m_subHelps:
            print ' %s : %s'%(subHelp.m_name,subHelp.m_helpInfo)
#########################################
def main():
    exeList = CExeList()
    exeList.loadFromXmlFile("exeCfg.xml")
    
    exeInvoker = CExeInvoker(exeList)
    print "WelCome to CMD invoke, using 'help' or 'help $CMD' refer to help"
    while True:
        strCmd = raw_input("%s > "%(time.asctime(time.localtime())))
        strCmdList = strCmd.split()
        if strCmdList[0] == 'quit' :
            break;     
        if strCmdList[0] == 'help':
            if len(strCmdList) > 1:
                exeInvoker.helpOneExe(strCmdList[1])
            else:
                exeInvoker.helpAllExe()
        if strCmdList[0] == 'run':
            strCmdList.remove(strCmdList[0])
            strCmdName = strCmdList[0]
            strCmdList.remove(strCmdList[0])
            exeInvoker.runOneExe(strCmdName,strCmdList)
            
        
    return
    
main()